package com.cskaoyan.demo.service;

import com.cskaoyan.demo.bean.ItemsBean;
import com.cskaoyan.demo.bean.User;
import com.cskaoyan.demo.bean.UserExample;
import com.cskaoyan.demo.mapper.UserMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.script.ScriptEngine;
import java.util.List;
import com.github.pagehelper.PageHelper;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;
    @Override
    public ItemsBean<User> queryList(Integer page, Integer limit,
                                     String username, String mobile,
                                     String sort, String order) {
        //分页等下做
        PageHelper.startPage(page, limit);

        // 查询
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (username != null){
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null){
            criteria.andMobileEqualTo(mobile);
        }
        userExample.setOrderByClause(sort + " " + order);

        List<User> users = userMapper.selectByExample(userExample);

        //int total = users.size();
        PageInfo<User> userPageInfo = new PageInfo<User>(users);
        int total = (int) userPageInfo.getTotal(); //会执行上面结果为users的这个查询所对应的select count（*） ,没有拼接分页

        return ItemsBean.data(users,total);
    }
}
